package my.mypool.core.ttcache;

import my.mypool.core.http.HttpClient;
import my.mypool.core.memcache.MemCacheClient;
import my.myspring.tool.SpringUtil;

import java.util.Map;
import java.util.HashMap;
import java.net.URLEncoder;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-9-21
 * Time: 10:55:56
 * To change this template use File | Settings | File Templates.
 */
public class TTCacheClient extends MemCacheClient {
    private HttpClient httpClient=null;
    public TTCacheClient(String host,int port,int maxIdle, int initIdleCapacity) throws Exception {
            super(host,port,maxIdle,initIdleCapacity);
            httpClient=new HttpClient(host,port,maxIdle,initIdleCapacity);
    }



    public boolean append(String key,String value,long expire) throws Exception {
         key=encodeValue(key);
         value=encodeValue(value);
         Map<String,String> headers=new HashMap<String,String>();
         headers.put("X-TT-PDMODE", "2");
         String rt=httpClient.doPut(URLEncoder.encode(key,"utf-8"),value+getSPLITSTR(),null,headers);
         return "Created\n".equals(rt);
    }

    public boolean appendDel(String key, String value, long expire) throws Exception {
        key=encodeValue(key);
        value=encodeValue(value);
        return append(key, getDELPREFIXSTR() + value, expire);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(SpringUtil.getBean("TTCacheClient", TTCacheClient.class).delete("collect_hxpterry",0));
      /*  while(true){
        System.out.println(new TTCacheClient("192.168.21.141",11211).append("黄小平001","测试2",0));
        }*/
        System.out.println(new TTCacheClient("192.168.21.141",11211,5,10).stat());
    }
    
}
